<script setup lang="ts">
import {onMounted, ref} from "vue";
import myAxios from "../plugins/myAxios"
import UnLoginInfoPage from "./UnLoginInfoPage.vue";
import {getCurrentUser} from "../service/user.ts";
import {Dialog, Toast} from "vant";

/**
 * const user = {
 *   id: 2501,
 *   username: "liquor",
 *   userAccount: "admin",
 *   avatarUrl: "https://www.mianshiya.com/_next/image?url=%2Flogo.png&w=32&q=75",
 *   gender: "男",
 *   phone: "18856957326",
 *   email: "rose@qq.com",
 *   planetCode: "0001",
 *   createTime: new Date()
 * };
 */

const user = ref(null)
const loading = ref(true)

onMounted(async () => {
  const res = await myAxios.get('user/current');
  loading.value = false
  if (res.code === 0) {
    user.value = res.data;
  }
})


const logout = async () => {
  const currentUser = await getCurrentUser();

  if (!currentUser) {
    return;
  }
  const res = await myAxios.post('/user/logout',{
    'id': currentUser.id,
  });
  if (res.code === 0 && res.data === 1) {
    Toast.success("退出成功！")
    await new Promise(resolve => setTimeout(resolve, 800))
    location.reload()
  } else {
    Toast.fail("退出失败！")
  }
}

const showMsg_1 = () => {
  Dialog({
    title:'系统说明',
    message: '这是一款可以匹配相同兴趣伙伴的交友软件，可以与陌生人进行线上pk、游戏等一系列社交活动！' });
}
const showMsg_2 = () => {
  Dialog({
    title:'用户协议',
    message: '该系统仅供学习参考使用，盗版必究！' });
}
const showMsg_3 = () => {
  Dialog({
    title:'版本信息',
    message: '已经是最新版本v1.0' });
}

</script>

<template>
  <div class="loading-wrapper" v-if="loading">
    <van-loading type="spinner" size="36px">加载中...</van-loading>
  </div>

  <template v-else>
    <template v-if="user">
      <van-cell title="当前用户" :value="user?.username" />
      <van-cell title="修改信息" is-link to="/user/update" />
      <van-cell title="我创建的队伍" is-link to="/user/team/create" />
      <van-cell title="我加入的队伍" is-link to="/user/team/join" />
      <van-cell title="系统说明" is-link @click="showMsg_1"/>
      <van-cell title="用户协议" is-link @click="showMsg_2"/>
      <van-cell title="版本信息" is-link @click="showMsg_3"/>
      <van-cell title="修改密码" is-link to = "/user/pwdUpdate"/>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit" @click="logout">
          退出登录
        </van-button>
      </div>
    </template>
    <template v-else>
      <UnLoginInfoPage />
    </template>
  </template>
</template>

<style scoped>
.loading-wrapper {
  position: fixed; /* 覆盖整个页面 */
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex; /* Flex 居中 */
  justify-content: center;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.8); /* 可选：加半透明遮罩 */
  z-index: 9999;
}/* 确保覆盖在最上层 */
</style>